package OrderBaseService

import (
	"context"
	"gdshop-font-api-go/app/response/OrderResp"
	"gdshop-font-api-go/library/tools"
	toolsDb "gdshop-font-api-go/library/tools/db"
	"github.com/gogf/gf/frame/g"
)

func GetPager(ctx context.Context, where g.Map, pageIndex, pageSize, status int, sort string) (*OrderResp.List, error) {
	countDb := toolsDb.GetUnSafaTableAddDeleteWhere(
		ctx,
		"order").Where(where).Fields(
		"1")
	listDb := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "order").Page(
		pageIndex, pageSize,
	).Order("id DESC").Where(where)
	if status > 0 {
		if status == 3 {
			countDb.Where("status IN (?)", []int{2, 3})
			listDb.Where("status IN (?)", []int{2, 3})
		} else {
			countDb.Where("status=?", status)
			listDb.Where("status=?", status)
		}
	}
	var orderList []*OrderResp.Details
	count, countErr := countDb.Count()
	if countErr != nil {
		return nil, countErr
	}

	err := listDb.Structs(&orderList)

	if err != nil {
		return nil, err
	} else {
		orderRespList := OrderResp.List{
			PageInfo:       tools.GetPageInfo(count, pageIndex, pageSize),
			NextStatusText: GetNextStatusText(),
			StatusText:     GetStatusText(),
			PhotoBaseUrl:   g.Cfg().GetString("site.PhotoUrlProfix"),
		}
		if count > 0 {
			// 处理返回数据
			for _, item := range orderList {
				HandleDetails(ctx, item)
			}

			orderRespList.List = orderList
		} else {
			orderRespList.List = []*OrderResp.Details{}
		}

		return &orderRespList, nil
	}
}
